using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Dracham.GUI.Controls
{
    public class TextViewer : Control
    {
        public class TextString
        {
            private String text = "";
            private Color textColor = Color.Black;

            #region properties 

            public String Text
            {
                get { return text; }
                set { text = value; }
            }

            public Color TextColor
            {
                get { return textColor; }
                set { textColor = value; }
            }

            #endregion

            public TextString(String text, Color textColor)
            {
                this.text = text;
                this.textColor = textColor;
            }
        }

        private Rectangle destination;
        private SpriteFont font;
        private List<TextString> text;
        private Texture2D texture;
        private Single charWidth = 0;
        private Single charHeight = 0;

        public Rectangle Destination
        {
            get { return destination; }
        }

        public List<TextString> Text
        {
            get
            {
                return this.text;
            }
        }

        public TextViewer(String name, Rectangle desinationRectangle, String fontName)
        {
            this.text = new List<TextString>();

            this.Name = name;
            this.destination = desinationRectangle;
            this.texture = GUIHelper.Content.Load<Texture2D>("gui\\textbg");
            this.font = GUIHelper.Content.Load<SpriteFont>("fonts\\" + fontName);
            this.charWidth = this.font.MeasureString("h").X;
            this.charHeight = this.font.MeasureString("h").Y;
        }

        public void Render(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.texture, this.destination, Color.White);

            List<TextString> newString = new List<TextString>();
            Int32 charsPerLine = (Int32)((Int32)this.destination.Width / charWidth) - 1;
            Int32 numOfLines = (Int32)((Int32)this.destination.Height / charHeight);

            for (Int32 i = 0; i < text.Count; i++)
            {
                String l = text[i].Text;
                float totalLength = (l.Length * charWidth);

                if (totalLength > this.destination.Width)
                {
                    Int32 newLines = l.Length / charsPerLine;

                    for (Int32 x = 0; x <= newLines; x++)
                    {
                        newString.Add(new TextString(l.Substring(x * charsPerLine, Math.Min(charsPerLine, l.Length - (x * charsPerLine))), text[i].TextColor));
                    }
                }
                else
                {
                    newString.Add(new TextString(l, text[i].TextColor));
                }
            }

            List<TextString> textToShow = new List<TextString>();
            if (newString.Count > numOfLines)
            {
                for (Int32 x = 1; x <= (numOfLines); x++)
                {
                    textToShow.Insert(0, newString[newString.Count - x]);
                }
            }
            else { textToShow = newString; }

            //spriteBatch.DrawString(this.font, String.Join("\r\n", textToShow.ToArray()), new Vector2(this.destination.Left + 3, this.destination.Top + 3), Color.Black);
            Int32 line = 0;
            foreach (TextString ts in textToShow)
            {
                spriteBatch.DrawString(this.font, ts.Text, new Vector2(this.destination.Left + 3, this.destination.Top + 3 + (line * this.charHeight + 1)), ts.TextColor);
                line++;
            }
        }

        public override void Update(GameTime gameTime, MouseState mouseState, KeyboardState keyboardState)
        {

        }
    }
}
